/****************************************
 * 文件名：   sms.c
 * 作者：     fengjunhui
 * 修改时间： 3018-1-2
 * 文件说明： 串口应用编程之发送短信
 ****************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <fcntl.h>
#include <termios.h>
#include "uart_api.h"

int gprs_fd;
char *tel_num="18883765905";
char *message ="helloxiaohui";

void send_message(char *tel,char *msg)
{
	printf("enter send_message .....\n");
	int n=0,ret=0,num=0;
	char end=26;      //发送短信的结束符号
	char buff[128]={0};

	memset(buff,0,128);//测试串口是否正常工作，尽可能的多判断返回值
	strcpy(buff,"AT\n");
	write (gprs_fd,buff,strlen(buff));
	read(gprs_fd,buff,128);		
	if(strncmp(buff,"OK",2)){
		printf("serial port connected success \n");	
	}else{
		printf("serial port connected failed  \n");
	}	
	usleep(10000);		


	memset(buff,0,128);								
	strcpy(buff,"AT+CPIN?\n");//查询是否检测到SIM卡
	write (gprs_fd,buff,strlen(buff));          								
	read(gprs_fd,buff,128);
	printf("set coding type success,SIM卡成功检测到.\n");				
	usleep(10000);

	memset(buff,0,128);									
	strcpy(buff,"AT+CMGF=1\n");         //设置短信发送模式
	write (gprs_fd,buff,strlen(buff));  // CMGF :0(默认)：PDU模式 1：文本模式
	read(gprs_fd,buff,128);
	if(strncmp(buff,"OK",2)){
		printf("set message type success \n");
	}else{
		printf("set message type failed\n");
	}				
	usleep(100000);

	memset(buff,0,128);
	strcpy(buff,"AT+CSMP=17,167,0,240\n");         //参数4：241 短信存在sim卡中 240直接在终端显示			
	write (gprs_fd,buff,strlen(buff));
	read(gprs_fd,buff,128);usleep(10000);
	printf("message save at sim card!\n");
	usleep(100000);

	memset(buff,0,128);
	strcpy(buff,"AT+CMGDA=6\n");        			 //删除之前发送的信息与指令		
	write (gprs_fd,buff,strlen(buff));					
	read(gprs_fd,buff,128);
	if(strncmp(buff,"OK",2)){
		printf("delect message success!\n");				
	}else{
		printf("delect failed!\n");		
	}						

	memset(buff,0,128);
	sprintf(buff,"AT+CMGS=\"%s\"",tel_num);  //发送短信
	strcat(buff,"\n");
	write (gprs_fd,buff,strlen(buff));
	read(gprs_fd,buff,128);
	if(strncmp(buff,">",1))
	{
		if(message=='\0')
		{
			printf("please input the message information :");					
			scanf("%s",buff);					
			write(gprs_fd,buff,strlen(buff));					
			read(gprs_fd,buff,128);	
			write(gprs_fd,&end,1);
			read(gprs_fd,buff,128);
		}else{
			strcpy(buff,message);
			write(gprs_fd,buff,strlen(buff));					
			read(gprs_fd,buff,128);	
			write(gprs_fd,&end,1);
			read(gprs_fd,buff,128);	
		}

		n=strlen(buff);
		if(strncmp(&buff[n-1],"O",1)&&strncmp(&buff[n],"K",1)){
			printf("send message success :%s\n",msg);
		}else{
			printf("send message failed\n");
		}
		sleep(1);
	}else{					
		printf("send message failed!\n");
	}	
	sleep(2);
	return ;
}


int main()
{
	char buff[128]={0};
	//	char sbuff[128]={0};
	//	打开串口
	if ((gprs_fd = open_port("/dev/ttyUSB1")) < 0){
		perror("open_port");
		return 1;
	}

	printf("gprs_fd :%d.\n",gprs_fd);
	
	//设置波特率，奇偶校验位和停止位
	if (set_com_config(gprs_fd, 9600, 8, 'N', 1) < 0){
		perror("set_com_config");
		return 1;
	}

	sleep(1);
	//设置好之后开始发送短信
	send_message(tel_num,message);
	//发完短信之后关闭
	close(gprs_fd);
	return 0;
}
